#!/usr/bin/env pwsh
# MySQL连接检查脚本

Write-Host "=== MySQL连接检查 ===" -ForegroundColor Green

# 检查MySQL服务状态
Write-Host "`n1. 检查MySQL服务状态..." -ForegroundColor Yellow
try {
    $mysqlService = Get-Service -Name "MySQL*" -ErrorAction SilentlyContinue
    if ($mysqlService) {
        Write-Host "MySQL服务状态: $($mysqlService.Status)" -ForegroundColor Cyan
        if ($mysqlService.Status -ne "Running") {
            Write-Host "MySQL服务未运行，尝试启动..." -ForegroundColor Red
            Start-Service $mysqlService.Name
        }
    } else {
        Write-Host "未找到MySQL服务，请检查MySQL是否已安装" -ForegroundColor Red
    }
} catch {
    Write-Host "无法检查MySQL服务状态: $($_.Exception.Message)" -ForegroundColor Red
}

# 检查端口3306是否开放
Write-Host "`n2. 检查MySQL端口3306..." -ForegroundColor Yellow
try {
    $connection = Test-NetConnection -ComputerName "localhost" -Port 3306 -WarningAction SilentlyContinue
    if ($connection.TcpTestSucceeded) {
        Write-Host "端口3306连接成功" -ForegroundColor Green
    } else {
        Write-Host "端口3306连接失败" -ForegroundColor Red
    }
} catch {
    Write-Host "端口测试失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 提供解决方案
Write-Host "`n=== 解决方案 ===" -ForegroundColor Green
Write-Host "如果MySQL连接失败，请按以下步骤操作：" -ForegroundColor White
Write-Host "1. 确保MySQL已安装并启动" -ForegroundColor White
Write-Host "2. 检查用户名密码是否正确 (当前配置: root/root)" -ForegroundColor White
Write-Host "3. 确保数据库'mall'已创建" -ForegroundColor White
Write-Host "4. 检查防火墙设置" -ForegroundColor White
Write-Host "5. 如果使用Docker，确保容器正在运行" -ForegroundColor White

Write-Host "`n=== 快速启动MySQL (Docker方式) ===" -ForegroundColor Green
Write-Host "docker run -d --name mysql-mall -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=mall mysql:8.0" -ForegroundColor Cyan